找传奇、传世资源到传世资源站!

Java飞机大战(射击类小游戏源码,带声音效果)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

Java小游戏的代码,图片都有啦,shoot game
from clipboard
from clipboardpackage shoot;import java.applet.Applet;import java.awt.Color;import java.awt.Font;/* * * 游戏面板类 */import java.awt.Graphics;import java.awt.Image;import java.awt.Toolkit;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.io.File;import java.net.MalformedURLException;import java.util.ArrayList;import java.util.Random;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JLabel;import javax.swing.JPanel;import com.sun.media.jfxmedia.AudioClip;import com.sun.org.apache.xerces.internal.impl.xpath.XPath.Step;import com.sun.org.apache.xml.internal.security.utils.IgnoreAllErrorHandler;import shoot.Bee;import shoot.FlyingObject;import javafx.beans.binding.When;import javazoom.jl.player.Player;public class Gamepanel extends JPanel implements Runnable,MouseMotionListener,MouseListener{public static final int PANEL_WIDTH=GameFrame.FRAME_WIDTH-7;public static final int PANEL_HEIGHT=GameFrame.FRAME_HEIGHT-30;public static final int MAX_EPLANE_NUMBER=6;private Background background;//天空背景对象private HeroPlane heroPlane;//英雄机对象private ArrayList<EnemyPlane> ePlanes;//敌机对象private ArrayList<Enemybullet>enemybullets;//敌机子弹类对象private ArrayList<HeroBullet> heroBullets;//英雄机子弹对象private ArrayList<Boom> booms;//爆破对象private Bee bee;//小蜜蜂对象private int total_score=0;//游戏总得分//AudioClip对象提供 play loop(循环) stop java.applet.AudioClip all_bomb,enemy_bomb,bg,hero_bomb,hero_bullet; public Gamepanel() { // Image img=Toolkit.getDefaultToolkit().getImage("fish08_01.png"); //初始化音乐 try {all_bomb=Applet.newAudioClip(new File("music/all_bomb.wav").toURI().toURL());enemy_bomb=Applet.newAudioClip(new File("music/enemy_bomb.wav").toURI().toURL());bg=Applet.newAudioClip(new File("music/bg.wav").toURI().toURL());hero_bomb=Applet.newAudioClip(new File("music/hero_bomb.wav").toURI().toURL());hero_bullet=Applet.newAudioClip(new File("music/hero_bullet.wav").toURI().toURL()); } catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} //初始化背景 background=new Background(); //初始化敌机 ePlanes=new ArrayList<EnemyPlane>(); for(int i=0;i<MAX_EPLANE_NUMBER;i ) { ePlanes.add(new EnemyPlane()); } //初始化英雄机 heroPlane=new HeroPlane(); //初始化敌机 子弹 enemybullets=new ArrayList<Enemybullet>(); //初始化英雄机子弹 heroBullets=new ArrayList<HeroBullet>(); //初始化爆破效果 booms=new ArrayList<Boom>(); //初始化小蜜蜂 bee=new Bee(); //添加鼠标监听器 this.addMouseMotionListener(this); this.addMouseListener(this); } @Override//重写有两种重写,一个是全部重写,一个是追加式的重写,就像super。 protected void paintComponent(Graphics g) { // TODO Auto-generated method stub super.paintComponent(g); //画天空 background.draw(g); //画敌机 for (int i = 0; i <ePlanes.size(); i ) { EnemyPlane enemyPlane=ePlanes.get(i); enemyPlane.draw(g);} //画英雄机 heroPlane.draw(g); //画敌机子弹 for (int i=0;i<enemybullets.size();i ) { Enemybullet enemybullet=enemybullets.get(i); enemybullet.draw(g); } //画英雄机子弹 for (int i = 0; i < heroBullets.size(); i ) {HeroBullet heroBullet=heroBullets.get(i);heroBullet.draw(g);} //画爆破效果 for(int i=0;i<booms.size();i ) { Boom boom=booms.get(i); boom.draw(g); } //画小蜜蜂 bee.draw(g); //画分和命 g.setColor(Color.blue); g.setFont(new Font("微软雅黑",Font.BOLD,30)); g.drawString("命数:" heroPlane.getLife(), 10, 30); g.drawString("得分:" total_score, 10, 80); if (heroPlane.getLife()==0) { g.setColor(Color.red); g.setFont(new Font("微软雅黑",Font.BOLD,50)); g.drawString("Game over!", PANEL_WIDTH/3-40, PANEL_HEIGHT/2); } }@Overridepublic void run() {bg.play();//while (true) {////for(int i=1;i<=10;i ) {// x =3;//img=Toolkit.getDefaultToolkit().getImage("fish08_0" i ".png");//repaint();//重画,自动调用paint//while(x==350) {////x=-100;//y=(int)(Math.random()*250);//}//try {//Thread.sleep(50);//} catch (InterruptedException e) {//// TODO Auto-generated catch block//e.printStackTrace();//}//}}while(true) {//1背景图移动background.move();//2对敌机的处理for (int i = 0; i < ePlanes.size(); i ) { EnemyPlane enemyPlane=ePlanes.get(i); //2.1敌机移动 enemyPlane.move(heroPlane); //2.2敌机发射子弹 Enemybullet enemybullet=enemyPlane.shoot(); if (enemybullet!=null) { enemybullets.add(enemybullet);} }//3对敌机子弹的处理for (int i=0;i<enemybullets.size();i ) { Enemybullet enemybullet=enemybullets.get(i); //3.1移动 enemybullet.move(); //3.2是否出界 if (enemybullet.outOfBounds()) {enemybullets.remove(i);i--;}}//对蜜蜂处理bee.move();if (bee.outofBounds()) {bee.y=-4000;Random rand = new Random();bee.x=rand.nextInt(PANEL_WIDTH - this.WIDTH);}//4对英雄机子弹的处理//4.1移动for (int i = 0; i < heroBullets.size(); i ) {HeroBullet heroBullet=heroBullets.get(i);heroBullet.move();//4.2是否越界if (heroBullet.outOfBounds()) {heroBullets.remove(i);i--;}}//5.碰撞检测//5.1 检测敌机与英雄机,英雄机子弹的碰撞情况boolean heroPlane_ishitted=false;//记录英雄机被撞 for (int i = 0; i < ePlanes.size(); i ) { EnemyPlane enemyPlane=ePlanes.get(i); boolean ePlane_isHitted=false;//记录敌机被撞 //5.1.1跟英雄机撞 if (enemyPlane.isHitted(heroPlane)) { ePlane_isHitted=true; Boom boom1=new Boom(heroPlane.x,heroPlane.y,heroPlane.width,heroPlane.height,heroPlane.xStep,heroPlane.yStep); booms.add(boom1); heroPlane.setLife(heroPlane.getLife()-1);//生命数-1 //加上heroplane的高,为了防止出界后的敌机与英雄机相撞 heroPlane.move(0,Gamepanel.PANEL_HEIGHT heroPlane.height); //移除鼠标监听器,暂时无法移动 this.removeMouseListener(this); this.removeMouseMotionListener(this); heroPlane.setRestatrt_delay_count(40);//设置英雄机重启,需要延迟40个50ms(参见thread.sleep) heroPlane.setCurrent_live(false); } //5.1.2跟英雄机子弹是否相撞 for(int j=0;j<heroBullets.size();j ) { HeroBullet heroBullet=heroBullets.get(j); if (enemyPlane.isHitted(heroBullet)) {ePlane_isHitted=true;heroBullets.remove(j);//移除英雄机子弹j--;} //蜜蜂撞上了 if (bee.isHitted(heroBullet)) { Random random=new Random(); bee.y=-2000; bee.x=random.nextInt(PANEL_WIDTH)-80; heroBullets.remove(j); i--; heroPlane.setLife(heroPlane.getLife() 1); } } if (ePlane_isHitted) {//若敌机被撞 total_score =enemyPlane.getScore(); hero_bullet.play(); ePlanes.remove(i);//移除该敌机 i--; Boom boom=new Boom(enemyPlane.x,enemyPlane.y,enemyPlane.width,enemyPlane.height,enemyPlane.xStep,enemyPlane.yStep); booms.add(boom); } } //5.2检测英雄机和敌机子弹的碰撞情况 //关于英雄机和敌机碰撞,见5.1 for( int i=0; i<enemybullets.size();i ) { Enemybullet enemybullet=enemybullets.get(i); if (heroPlane.isHitted(enemybullet)) {heroPlane_ishitted=true;enemybullets.remove(i);//移除该子弹i--;} } if (heroPlane_ishitted) {//如果英雄机被撞了 hero_bomb.play();Boom boom=new Boom(heroPlane.x,heroPlane.y, heroPlane.width,heroPlane.height,heroPlane.xStep,heroPlane.yStep);booms.add(boom);heroPlane.setLife(heroPlane.getLife()-1);//生命数-1//加上heroplane的高,为了防止出界后的敌机与英雄机相撞heroPlane.move(0,Gamepanel.PANEL_HEIGHT heroPlane.height);//移除鼠标监听器,暂时无法移动 this.removeMouseListener(this); this.removeMouseMotionListener(this); heroPlane.setRestatrt_delay_count(40);//设置英雄机重启,需要延迟40个50ms(参见thread.sleep) heroPlane.setCurrent_live(false); }//6对爆破对象进行处理 for (int i = 0; i < booms.size(); i ) { Boom boom=booms.get(i); boom.move(); if (boom.booms_end()) {//该爆破结束 booms.remove(i);} } //7对英雄机状态处理 if (heroPlane.getLife()>0) { if (heroPlane.getRestatrt_delay_count()>0) {heroPlane.setRestatrt_delay_count(heroPlane.getRestatrt_delay_count()-1);} //如果英雄机当前死亡,且重启延时已经为0 if ( heroPlane.isCurrent_live()==false && heroPlane.getRestatrt_delay_count()==0) { heroPlane.move(Gamepanel.PANEL_WIDTH/2-35, Gamepanel.PANEL_HEIGHT-70); heroPlane.setCurrent_live(true); addMouseListener(this); addMouseMotionListener(this); //添加一个全屏爆炸 all_bomb.play(); Boom boom=new Boom(0, 0, PANEL_WIDTH, PANEL_HEIGHT, 0, 0); booms.add(boom); for(int i =0;i<ePlanes.size();i ) {//清除屏幕得分 EnemyPlane ePlane=ePlanes.get(i); total_score =ePlane.getScore(); } ePlanes.clear();//清空所有敌机 enemybullets.clear();//清空所有子弹 } } //逐渐补充敌机数到MAX_EPLAN_NUMBER if (ePlanes.size()<MAX_EPLANE_NUMBER) {ePlanes.add(new EnemyPlane());} if (heroPlane.getLife()==0) {bg.stop();} repaint();try {Thread.sleep(30);//30ms} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}@Overridepublic void mouseDragged(MouseEvent e) {heroPlane.move(e.getX(),e.getY());}@Overridepublic void mouseMoved(MouseEvent e) {//System.out.println(e.getX() "," e.getY());heroPlane.move(e.getX(),e.getY());}@Overridepublic void mouseClicked(MouseEvent e) {}@Overridepublic void mousePressed(MouseEvent e) {hero_bullet.play();HeroBullet heroBullet=heroPlane.shoot();heroBullets.add(heroBullet);}@Overridepublic void mouseReleased(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mouseEntered(MouseEvent e) {}@Overridepublic void mouseExited(MouseEvent e) {// TODO Auto-generated method stub}}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复